﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BDGetQuote.Web.Models
{
    public class CategoryRepository : ICategoryRepository
    {
        public IList<Category> GetAll()
        {
            using (var _dbcontext = new BDQuoteDBDataContext())
            {
                var result = _dbcontext.BO_Categories.Where(x => !x.IsDeleted).OrderBy(x => x.Name).Select(
                    x => new Category()
                    {
                        CategoryId = x.CategoryId,
                        Name = x.Name,
                        Description = x.Description,
                        ImageUrl = x.ImageUrl
                    }).ToList();
                return result;
            }
        }

        public IList<Category> GetAll(string userId)
        {
            using (var _dbcontext = new BDQuoteDBDataContext())
            {
                var result = _dbcontext.BO_Categories.Where(x => !x.IsDeleted).OrderBy(x => x.Name).Select(
                    x => new Category()
                    {
                        CategoryId = x.CategoryId,
                        Name = x.Name,
                        Description = x.Description,
                        ImageUrl = x.ImageUrl,
                        TotalProducts = x.BO_Products.Count(p => !p.IsDeleted && (p.IsSystem || p.CreatedBy == userId))
                    }).ToList();
                return result;
            }
        }
    }
}